<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .done {
        color: gray;
        text-decoration: line-through;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <input type="text" v-model="title" @keydown.enter="addTodo" />
      <button v-if="left < all" @click="clear">清理</button>
      <ul v-if="todos.length">
        <li v-for="todo in todos" :key="todo.title">
          <input type="checkbox" v-model="todo.done" />
          <span :class="{done: todo.done}"> {{todo.title}}</span>
        </li>
      </ul>
      <div v-else>暂无数据</div>
      全选<input type="checkbox" v-model="allDone" />:&emsp;{{left}}/{{all}}
    </div>
    <script src="./lib/vue.global.js"></script>
    <script>
      const app = {
        data() {
          return {
            title: "",
            todos: [],
          };
        },
        computed: {
          left() {
            return this.todos.filter((o) => !o.done).length;
          },
          all() {
            return this.todos.length;
          },
          allDone: {
            get() {
              return this.left === 0;
            },
            set(val) {
              this.todos.forEach((o) => {
                o.done = val;
              });
            },
          },
        },
        methods: {
          addTodo() {
            this.todos.push({
              title: this.title,
              done: false,
            });
            this.title = "";
          },
          clear() {
            this.todos = this.todos.filter((o) => !o.done);
          },
        },
      };

      Vue.createApp(app).mount("#app");
    </script>
  </body>
</html>
